using Gameplay.RTS.Hud;
using TMPro;
using UnityEngine;

namespace WorldMapBase.Tile.Pool
{
    public class WorldFlyTextPool : ObjectPool<WorldFlyText>
    {
        private int _initCount;
        private int _flyTextType;
        private GameObject _originPrefab;

        public WorldFlyTextPool(int initCount,int capacity,GameObject prefab,int type) :
            base(capacity, true,1,5,10000)
        {
#if UNITY_EDITOR
            _name = "飘字对象池: " + prefab.name;
#endif
            _initCount = initCount;
            _flyTextType = type;
            _originPrefab = prefab;
            //修改prefab的sortOrder，同类型的飘字合批
            var m_text = prefab.GetComponentInChildren<TMP_Text>();
            if (m_text!=null)
            {
                m_text.SetActive(true);
                m_text.raycastTarget = false;
                m_text.alpha = 0;
                //设为overlay
                //m_text.isOverlay = true;
                m_text.fontSharedMaterial.SetFloat(ShaderUtilities.ShaderTag_ZTestMode, 0);
                // m_text.sortingOrder = 9000 + type;
                //m_text.isOverlay = true;
            }
        }


        public void PreInit()
        {
            base.OnInitAsync(_initCount);
        }
        
        protected override WorldFlyText ConstructItem()
        {
            WorldFlyText worldFlyText = new WorldFlyText();
            worldFlyText.Create(_originPrefab,_flyTextType);
            return worldFlyText;
        }
        private readonly Vector3 INVISIBLE_POSITION = new Vector3(-99999, -99999, -99999);

        protected override void BeforeRecover(WorldFlyText obj)
        {
            obj.FlyText.transform.position = INVISIBLE_POSITION;
            obj.recover();
        }

        
        public override void Destroy()
        {
            if (_originPrefab != null)
            {
                base.Destroy();
                _originPrefab = null;
            }
          
        }
    }
}